<?php
/**
 * Albireo Kernel
 *
 * @copyright  Copyright (c) 2010 Albireo Solutions
 * @package    Kernel
 */
namespace KernelModule\AclModule;

use Nette\Environment;
use Nette\VisualPaginator;
use Nette\Forms\SubmitButton;
use Albireo\Forms\AlbireoForm;

/**
 * Base class for all application presenters.
 *
 * @author Pavel
 */
abstract class BasePresenter extends \KernelModule\BasePresenter
{

    /**
     * @var string
     * persistent parameter // it's declared by the static function getPersistentParams()
     */
    public $search = "";

    public static function getPersistentParams() {
            return array('search');
    }

    public function startup() {
        parent::startup();

        define('TABLE_ACL', "acl");
        define('TABLE_PRIVILEGES', "privileges");
        define('TABLE_RESOURCES', "resources");
        define('TABLE_ROLES', "roles");
        define('TABLE_USERS', "users");
        define('TABLE_USERS_ROLES', "users_roles");
    }

    public function beforeRender() {
        $this->showMenu();
    }

    public function createComponentPaginator() {
        $visualPaginator = new VisualPaginator();
        $visualPaginator->paginator->setItemsPerPage(10);
        return $visualPaginator;
    }


    /**
     * Redirects back to the default action. There is redirect, not view set,
     * because of the possible f5 refresh.
     *
     * @param SubmitButton $button the cancel button
     */
    public function cancelClicked(SubmitButton $button) {
        $this->redirect('default');
    }

    /**
     * Adds Add, Edit and Cancel button to the given form.
     *
     * @param Form $form The form we want to add butons to.
     */
    protected function addFormButtons($form) {
        if ($this->getAction()=='add')
            $form->addSubmit('add', _('Add'));
        elseif ($this->getAction()=='edit') {
            $form->addHidden("id");
            $form->addSubmit('edit', _('Edit'));
        }
        else throw new \InvalidStateException(); //this should never happen
        $this->addCancelButton($form);
        $form->onSubmit[] = array($this, 'addOrEditSubmitted');
    }

    /**
     * Adds the Cancel button to the given form.
     *
     * @param Form $form The form we want to add buton to.
     */
    protected function addCancelButton($form) {
        $form->addSubmit("cancelClicked", _("Cancel"))
                ->setValidationScope(FALSE)
                ->onClick[] = array($this, "cancelClicked");
    }

    /**
     * Creates the generic delete form, which contains hidden field and 2 buttons.
     * 
     * @return Form the new created form for deleting entities
     */
    protected function createComponentDelete() {
        $form = new AlbireoForm();
        $form->addHidden("id");
        $form->addSubmit('delete', _('Delete'));
        $form->addSubmit('cancel', _('Cancel'));
        $form->onSubmit[] = array($this, 'deleteOnFormSubmitted');
        return $form;
    }

    /**
     * Creates the generic search form with the input identified by id "albireo-acl-search-input"
     *
     * @param <type> $name
     * @param <type> $entityType
     * @return <type>
     */
    protected function createSearchForm($name, $entityType) {
        /*
            // alternative w/o $entityType parameter
            $entName = strtolower(preg_replace("/.*\\\\(.*)sPresenter/", "$1", self::getReflection()));
            // \Nette\Debug::dump($entName);
            // entName is now "privilege", "resource" or "permission"
            
         */
        
        $form = new AlbireoForm($this, $name);
        $form->addText("searchinput", sprintf(_("Search for %s:"),$entityType), 30)->setHtmlId("albireo-acl-search-input");
        $form->addSubmit('search', _('Search'));
        $form->onSubmit[] = array($this, 'searchSubmitted');
        return $form;
    }
}